package utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.utils.ByteArray;
	
	public class Waveform extends Sprite
	{
		private var _sound:Sound;
		
		private var _byteArray:ByteArray;
		private var _bitmapData:BitmapData;
		private var _bitmap:Bitmap;
		private var _rect:Rectangle;
		
		private var sprite:Sprite;
		
		public function Waveform(sound:Sound, width:int, height:int)
		{
			return void;
			addChild(sprite = new Sprite());
			_sound = sound;
			_rect = new Rectangle(0, 0, 1, 0);
			_byteArray = new ByteArray();
			_bitmapData = new BitmapData(500, 512, false, 0xffffff);
			_bitmap = new Bitmap(_bitmapData);
			sprite.addChild(_bitmap);
			create();
		}
		
		private function create():void
		{
			
			var extract:Number = Math.floor((_sound.length / 1001) * 44100);
			var ratio:Number = _sound.length / _bitmapData.width;
			_sound.extract(_byteArray, extract);
			_byteArray.position = 0;
			var step:int = _byteArray.length / 4096;
			var step2:Number = _bitmapData.width / 4096;
			
			var left:Number;
			var right:Number;
			
			if (_byteArray.length == 0) {
				trace(">>>>>>>>>>>> byteArray.len == 0");
				return void;
			}
			for (var i:int = 0; i < 4096; i++)
			{
				_rect.x = i * step2;
				left = _byteArray.readFloat() * 500;
				right = _byteArray.readFloat() * 500;
				_byteArray.position = i * step;
				
				if (Math.abs(left) > Math.abs(right))
				{
					if (left > 0)
					{
						_rect.y = 128 - left;
						_rect.height = left;
					}
					else
					{
						_rect.y = 128;
						_rect.height = -left;
					}
				}
				else
				{
					if (right > 0)
					{
						_rect.y = 128 - right;
						_rect.height = right;
					}
					else
					{
						_rect.y = 128;
						_rect.height = -right;
					}
				}
				_bitmapData.fillRect(new Rectangle(100, 100, 1, left), 0xFF0000);
			}
		}
	}
}